package com.example.clock;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

public class ChangeYearDialog extends Dialog {

    private ListView listView;
    private List<String> data;
    private ChangeYearListAdapter adapter;
    private MainActivity mainActivity;

    private Context context;

    public ChangeYearDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public ChangeYearDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public void setMainActivity(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.change_year_dialog);

        listView = findViewById(R.id.year_change_listview);
        getData();
        adapter = new ChangeYearListAdapter(data,context);
        adapter.setListener(new ChangeYearListAdapter.OnItemClickListener() {
            @Override
            public void itemClicked(String year) {
                if (mainActivity!=null){
                    mainActivity.setYear(year);
                    dismiss();
                }
            }
        });
        listView.setAdapter(adapter);
    }

    private void getData() {
        if (data == null){
            data = new ArrayList<>();
        }
        for (int i = 2020 ; i < 2045 ; i++){
            data.add(String.valueOf(i));
        }
    }
}
